/*
EP3D is a real-time 3D planet engine , which in addition to providing 
substandard scene rendering and scene management, of course, it also 
provides some basic class libraries to build the entire virtual 
planet, or even the entire universe.

Copyright (C) 2010  Hongjiang Zhang	(zhjwyat@gmail.com)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "EP3DColor.h"


// Start of EP3D namespace section
namespace EP3D
{
/*************************************************************************
Construction
*************************************************************************/
Color::Color():r(0),g(0),b(0),a(0)
{
}


Color::Color(const Color& val)
:r(val.r), g(val.g), b(val.b), a(val.a)
{
}

Color::Color(const EP3D::f32 *pf)
:r(pf[0]), g(pf[1]), b(pf[2]), a(pf[3])
{
}

Color::Color(f32 red, f32 green, f32 blue, f32 alpha /* = 1.0f */)
:r(red), g(green), b(blue), a(alpha)
{
}

Color::Color(argb& val)
{
	const f32 f = 1.0f/255.0f;
	r = f * (f32)(unsigned char)(val>>16);
	g = f * (f32)(unsigned char)(val>>8);
	b = f * (f32)(unsigned char)(val>>0);
	a = f * (f32)(unsigned char)(val>>24);
}

Color::operator argb() const
{
	argb R = r >= 1.0f ? 0xff : r <= 0.0f ? 0x00 : (argb) (r * 255.0f + 0.5f);
	argb G = g >= 1.0f ? 0xff : g <= 0.0f ? 0x00 : (argb) (g * 255.0f + 0.5f);
	argb B = b >= 1.0f ? 0xff : b <= 0.0f ? 0x00 : (argb) (b * 255.0f + 0.5f);
	argb A = a >= 1.0f ? 0xff : a <= 0.0f ? 0x00 : (argb) (a * 255.0f + 0.5f);

	return (A << 24) | (R << 16) | (G << 8) | B;
}


Color::operator f32 *() const
{
	return (f32*)&a;
}


Color& Color::operator +=(const Color &val)
{
	r += val.r;
	g += val.g;
	b += val.b;
	a += val.a;

	return*this;
}


Color& Color::operator -=(const Color &val)
{
	r -= val.r;
	g -= val.g;
	b -= val.b;
	a -= val.a;

	return*this;
}


Color& Color::operator *=(const f32 f)
{
	r *= f;
	g *= f;
	b *= f;
	a *= f;

	return*this;
}

Color& Color::operator /=(const f32 f)
{
	f32 fInv = 1.0f/f;
	r *= fInv;
	g *= fInv;
	b *= fInv;
	a *= fInv;

	return*this;
}


Color Color::operator +() const
{
	return *this;
}


Color Color::operator -() const
{
	return Color(-r, -g, -b, -a);
}


Color Color::operator + (const Color& val) const
{
	return Color(r + val.r, g + val.g, b + val.b, a + val.a);
}


Color  Color::operator - (const Color& val) const
{
	return Color(r - val.r, g - val.g, b - val.b, a - val.a);
}


Color Color::operator * (const f32 f) const
{
	return Color(r * f, g * f, b * f, a * f);
}


Color Color::operator / (const f32 f) const
{
	f32 fInv = 1.0f/f;
	return Color(r * fInv, g * fInv, b * fInv, a * fInv);
}


Color operator * (const f32 f, const Color& val)
{
	return Color(f * val.r, f * val.g, f * val.b, f * val.a);
}


bool Color::operator == (const Color& val) const
{
	return r == val.r && g == val.g && b == val.b && a == val.a; 
}


bool Color::operator !=(const EP3D::Color &val) const
{
	return r != val.r || g != val.g || b != val.b || a != val.a;
}


Color* ColorLerp(Color* pOut, const Color* val1, const Color* val2, const f32 f)
{
	pOut->r = val1->r + f * (val2->r - val1->r);
	pOut->g = val1->g + f * (val2->g - val1->g);
	pOut->b = val1->b + f * (val2->b - val1->b);
	pOut->a = val1->a + f * (val2->a - val1->a);
	return pOut;
}

//////////////////////////////////////////////////////////////////////////////////
ColorRect::ColorRect()
:topleft()
,topright()
,bottomleft()
,bottomright()
{
}

ColorRect::ColorRect(const Color &color)
:topleft(color)
,topright(color)
,bottomleft(color)
,bottomright(color)
{
}

ColorRect::ColorRect(const Color &tl, const Color &tr, const Color &bl, const Color &br)
:topleft(tl)
,topright(tr)
,bottomleft(bl)
,bottomright(br)
{
}

void ColorRect:: SetAlpha(f32 alpha)
{
	topleft.a = topright.a = bottomleft.a = bottomright.a = alpha;
}

void ColorRect::SetLeftAlpha(f32 alpha)
{
	topleft.a = bottomleft.a = alpha;
}

void ColorRect::SetRightAlpha(f32 alpha)
{
	topright.a = bottomright.a = alpha;
}

void ColorRect::SetTopAlpha(f32 alpha)
{
	topleft.a = topright.a = alpha;
}

void ColorRect::SetBottomAlpha(f32 alpha)
{
	bottomleft.a = bottomright.a = alpha;
}

bool ColorRect::IsMonochromatic() const
{
	return topleft == topright &&
		topleft == bottomleft &&
		topleft == bottomright;
}

ColorRect ColorRect::GetSubRectangle(f32 left, f32 top, f32 right, f32 bottom)
{
	return ColorRect(
		GetColorAtPoint(Point(left, top)),
		GetColorAtPoint(Point(right, top)),
		GetColorAtPoint(Point(left, bottom)),
		GetColorAtPoint(Point(right, bottom)));
}

Color ColorRect::GetColorAtPoint(Point point)
{
	Color color1 = (topright - topleft) * point.x + topleft;
	Color color2 = (bottomright - bottomleft) * point.x + bottomleft;
	return Color((color2 - color1) * point.y + color1);
}

void ColorRect::SetColor(const Color& color)
{
	topleft = topright = bottomleft = bottomright = color;
}

}// End of EP3D namespace section

